package me.zhengjie.modules.other.rest;

import com.qiaofang.applet.service.other.domain.Assets;
import com.qiaofang.applet.service.other.service.AssetsService;
import com.qiaofang.applet.service.other.service.dto.AssetsDetailDto;
import com.qiaofang.applet.service.other.service.dto.AssetsDto;
import com.qiaofang.applet.service.other.service.dto.AssetsRequestDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author QF2019
 * @company qiaofang
 * @date 2020/9/12 11:35
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "内容：资讯管理")
@RequestMapping("/api/assets")
public class AssetsController {

    @Autowired
    private AssetsService assetsService;

    @Autowired
    private UserService userService;

    @GetMapping
    @AnonymousAccess
    @ApiOperation(value = "查询资讯服务列表", notes = "查询资讯服务列表")
    public ResponseEntity<Page<AssetsDetailDto>> list(AssetsRequestDto assetsRequestDto, Pageable pageable) {
        assetsRequestDto.setCorporationCode(SecurityUtils.getCorporationCode());
        List<String> cityCodeList = userService.getCityCodeList(SecurityUtils.getCurrentUsername());
        assetsRequestDto.setCityCodes(cityCodeList);
        Page<AssetsDetailDto> result = assetsService.findByParams(assetsRequestDto, pageable);
        return ResponseEntity.ok(result);
    }

    @PostMapping
    @ApiOperation(value = "新增资讯服务", notes = "新增资讯服务")
    public ResponseEntity<Object> create(@Validated @RequestBody AssetsDto assetsDto) {
        Assets assets = new Assets();
        BeanUtils.copyProperties(assetsDto, assets);
        assets.setCorporationCode(SecurityUtils.getCorporationCode());
        assetsService.saveAssets(assets);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping
    @ApiOperation("修改资讯服务")
    public ResponseEntity<Object> update(@Validated @RequestBody AssetsDto assetsDto){
        Assets assets = new Assets();
        BeanUtils.copyProperties(assetsDto, assets);
        assetsService.saveAssets(assets);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @ApiOperation(value = "删除资讯服务", notes = "删除资讯服务")
    public ResponseEntity<Object> remove(@Validated @RequestBody List<String> assetsIds) {
        if(!CollectionUtils.isEmpty(assetsIds)){
            assetsService.removeAssets(assetsIds.get(0));
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }
}